//
//  DKTabBar.m
//  BSBDJ
//
//  Created by 丁侃 on 16/4/23.
//  Copyright © 2016年 丁侃. All rights reserved.
//

#import "DKTabBar.h"

@implementation DKTabBar

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    //遍历子控件,重新布局
    
    NSInteger index = 0;
    
    CGFloat tabBarWidth = self.dk_width / 5;
    CGFloat tabBarHeight = self.dk_height;
    
    CGFloat btnWidth = tabBarWidth;
    CGFloat btnHeight = tabBarHeight;
    
    for (UIView *subV in self.subviews) {

//        DKLog(@"%@",subV);
        //UITabBarButton是私有的API,无法获取,只能通过此方式来获取类型
        if ([subV isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
        
            
            subV.frame = CGRectMake(tabBarWidth * index, 0, tabBarWidth, tabBarHeight);
            
            if (index >= 2) {
                
                subV.dk_x += tabBarWidth;
            }
            ++index;
        }
    }
    
    //添加自定义的按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateSelected];
    btn.frame = CGRectMake(2 * btnWidth, 0, btnWidth, btnHeight);
    //监听按钮点击
    [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btn];
}

-(void)click
{
    if (self.tabBarBtnBlock) {
        self.tabBarBtnBlock();
    }
}

@end
